<template>
	<view class="container">
		<!-- 内容区域包装器 -->
		<view class="content-wrapper">
			<!-- 搜索区域 -->
			<view class="search-box">
				<u-search placeholder="请输入关键搜索" v-model="formData.kw" :show-action="false" :clearabled="true"
					:animation="false" shape="round" bgColor="#F5F6FA" height="72rpx" searchIconColor="#999999"
					placeholderColor="#999999" @change="changekeword" :inputStyle="{
						color: '#333333',
						backgroundColor: '#F5F6FA',
					}"></u-search>
				<view class="add-btn" @click="handleToAdd">
					<u-icon name="plus-circle" size="40rpx" color="#FE4F04"></u-icon>
				</view>
			</view>
		</view>
		<view class="content-box">
			<view class="content-total">
				共 {{total}} 位成员，点击姓名可查看详情
			</view>
			<view class="empty-review" v-if="list.length == 0">
				<image src='../static/images/empty-review.png' mode="aspectFit"></image>
			</view>
			<view class="content-list">
				<view class="content-item" v-for="item in list" :key="item.id" @click="handleToDetail(item.id)">
					<view class="content-item-cell" style="margin-bottom: 8rpx;">
						<view class="content-item-name"> {{item.name}} </view>
						<view class="content-item-type"> {{item.role_text || '角色'}} </view>
					</view>
					<view class="content-item-cell" style="margin-bottom: 14rpx;">
						<view class="content-item-mobile"> {{item.mobile}} </view>
						<view class="content-item-date"> {{item.create_time_text || formatTime(item.create_time)}} </view>
					</view>
					<view class="content-item-cell" v-if="item.wechat">
						<view class="content-item-wechat"> 微信：{{item.wechat}} </view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>


<script>
	import {
		staffIndex
	} from '@/api/part2.js'

	export default {
		data() {
			return {
				formData: {
					page: 1,
					page_size: 10,
					kw: ''
				},
				total: 0,
				list: [],
				timerId:null
			};
		},
		onShow() {
			this.handleFilter()
		},
		onHide() {
			if(this.timerId) {
				clearInterval(this.timerId)
			}
		},
		async onReachBottom() {
			if(this.list.length < this.total) {
				this.formData.page++
				this.init()
			}
		},
		methods: {
			formatTime(timestamp, format = 'YYYY-MM-DD') {
			  if (!timestamp) return '';
			  
			  const date = new Date(timestamp * 1000);
			  const config = {
			    YYYY: date.getFullYear(),
			    MM: String(date.getMonth() + 1).padStart(2, '0'),
			    DD: String(date.getDate()).padStart(2, '0'),
			    HH: String(date.getHours()).padStart(2, '0'),
			    mm: String(date.getMinutes()).padStart(2, '0'),
			    ss: String(date.getSeconds()).padStart(2, '0')
			  };
			  
			  return format.replace(/(YYYY|MM|DD|HH|mm|ss)/g, match => config[match]);
			},
			changekeword() {
				if(this.timerId) {
					clearInterval(this.timerId)
				}
				let that = this
				this.timerId = setTimeout(()=>{
					that.handleFilter()
				},200)
			},
			handleFilter(){
				this.formData.page = 1
				this.list = []
				this.init()
			},
			init() {
				staffIndex(this.formData).then(res => {
					console.log(res.data.data);
					res.data.data.data.forEach((item) => {
						this.list.push(item)
					})
					this.total = res.data.data.total
				})
			},
			handleToAdd() {
				uni.navigateTo({
					url: '/pages-landlord/employee/detail'
				})
			},
			handleToDetail(id) {
				console.log(id)
				uni.navigateTo({
					url: '/pages-landlord/employee/detail?id=' + id
				})
			}
		},
	};
</script>

<style lang="less" scoped>
	.search-box {
		padding: 12rpx 20rpx 24rpx 32rpx;
		background-color: #fff;
		display: flex;
	}

	.add-btn {
		padding: 20rpx;
	}

	.content-box {
		padding: 0 32rpx;
	}

	.content-total {
		padding: 16rpx 0;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 22rpx;
		color: #999999;
	}

	.content-item {
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.01);
		border-radius: 24rpx;
		padding: 24rpx;
		margin-bottom: 24rpx;
	}

	.content-item-cell {
		display: flex;
		justify-content: space-between;
		align-items: center;

		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 26rpx;
		color: #666666;
		line-height: 36rpx;
	}

	.content-item-name {
		font-family: PingFangHK, PingFangHK;
		font-weight: 700;
		font-size: 30rpx;
		color: #222222;
		line-height: 42rpx;
	}

	.content-item-type {}

	.content-item-mobile {
		color: #444444;
	}

	.content-item-date {}

	.content-item-wechat {
		font-size: 20rpx;
		color: #999999;
		line-height: 28rpx;
	}
	.empty-review {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 48rpx 0;
	
		image {
			width: 328rpx;
			height: 208rpx;
			margin-bottom: 24rpx;
		}
	}
</style>